/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.controller.evaluation.planning;

import cn.ac.nercis.pes.community.utils.AppStore;
import cn.ac.nercis.pes.community.views.evaluation.planning.ContentConfirmView;
import cn.ac.nercis.pes.community.views.evaluation.planning.EvaluationRequireView;
import cn.ac.nercis.pes.community.views.evaluation.planning.TargetConfirmView;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.StackPane;
import lombok.extern.slf4j.Slf4j;

import java.net.URL;
import java.util.Objects;
import java.util.ResourceBundle;

/**
 * 方案编制管理控制器
 * @author zpy
 * @version 1.0.0
 */
@Slf4j
public class PlanningController implements Initializable {
    @FXML
    private StackPane page;

    @FXML
    private TabPane planningTab;
    @FXML
    private Tab targetTab;
    @FXML
    private Tab contentTab;
    @FXML
    private Tab requiredTab;

    private String currentSystemId;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        planningTab.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if(Objects.equals(newValue,targetTab)){
                var targetView = AppStore.getEvaluationView(this.currentSystemId, TargetConfirmView.class);
                if(targetView.getController() instanceof TargetConfirmController controller){
                    controller.initializeData(this.currentSystemId);
                }
            } else if (Objects.equals(newValue,contentTab)) {
                var contentView = AppStore.getEvaluationView(this.currentSystemId, ContentConfirmView.class);
                if(contentView.getController() instanceof ContentConfirmController controller){
                    controller.initializeData(this.currentSystemId);
                }
            } else {
                var requireView = AppStore.getEvaluationView(this.currentSystemId, EvaluationRequireView.class);
                if(requireView.getController() instanceof EvaluationRequireController controller){
                    controller.initializeData(this.currentSystemId);
                }
            }
        });
    }

    public void initializeData(String systemId){
        this.currentSystemId = systemId;
        var targetView = AppStore.getEvaluationView(this.currentSystemId, TargetConfirmView.class);
        var contentView = AppStore.getEvaluationView(this.currentSystemId, ContentConfirmView.class);
        var requireView = AppStore.getEvaluationView(this.currentSystemId, EvaluationRequireView.class);
        targetTab.setContent(Objects.requireNonNull(targetView).load());
        contentTab.setContent(Objects.requireNonNull(contentView).load());
        requiredTab.setContent(Objects.requireNonNull(requireView).load());
        if(Objects.isNull(planningTab.getSelectionModel().getSelectedItem())
                || Objects.equals(planningTab.getSelectionModel().getSelectedItem(),targetTab)){
            if(targetView.getController() instanceof TargetConfirmController controller){
                controller.initializeData(this.currentSystemId);
            }
        } else if (Objects.equals(planningTab.getSelectionModel().getSelectedItem(),contentTab)) {
            if(contentView.getController() instanceof ContentConfirmController controller){
                controller.initializeData(this.currentSystemId);
            }
        } else {
            if(requireView.getController() instanceof EvaluationRequireController controller){
                controller.initializeData(this.currentSystemId);
            }
        }
    }

    public StackPane getPage(){
        return this.page;
    }

}